# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from toolFunctions.prpcrypt.prpcrypt import Prpcrypt


class PasswordField(models.CharField):
    description = 'save password'

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 128
        # kwargs['default'] = 123123
        super(PasswordField, self).__init__(*args, **kwargs)

    #   在转成数据库查询前加密密码
    def get_db_prep_value(self, value, connection, prepared=False):
        value = super(PasswordField, self).get_db_prep_value(value, connection, prepared)
        if value is None:
            return value
        return Prpcrypt().encrypt(value)

    #   在保存到数据库前加密密码
    def get_db_prep_save(self, value, connection):
        if value is None:
            return value
        return Prpcrypt().encrypt(value)


#   user模型
class User (models.Model):
    id = models.AutoField(primary_key=True)
    password = PasswordField()
    username = models.CharField(max_length=150)
    email = models.EmailField()
    head_img = models.URLField()
    description = models.CharField(max_length=255, default='')
    is_active = models.BooleanField(default=True)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user'


#   收藏模型
class Collect (models.Model):
    id = models.AutoField(primary_key=True)

    user = models.ForeignKey('User', on_delete=models.CASCADE)
    article = models.ForeignKey('articleModel.Article', on_delete=models.CASCADE)

    class Meta:
        db_table = 'user_collect'



